package com.ts.system.service.impl;

import com.google.common.collect.Maps;
import com.ts.common.utils.Result;
import com.ts.common.utils.StringUtils;
import com.ts.system.dao.SystemParamsDao;
import com.ts.system.domain.SystemParamsDO;
import com.ts.system.service.SystemParamsService;
import com.ts.system.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SystemParamsServiceImpl implements SystemParamsService {
    @Autowired
    private SystemParamsDao systemParamsDao;

    @Override
    public SystemParamsDO get(Integer id) {
        return systemParamsDao.get(id);
    }

    @Override
    public List<SystemParamsDO> list(Map<String, Object> map) {
        return systemParamsDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return systemParamsDao.count(map);
    }

    @Override
    public int save(Result result, SystemParamsDO systemParams) {
        //add by bobby
        if (StringUtils.isBlank(systemParams.getParamKey())) {
            result.set(Result.FAIL, "参数key不能为空！", null);
            return 0;
        }
        Map map = Maps.newHashMap();
        map.put("paramKey", systemParams.getParamKey());
        if (systemParamsDao.list(map).size() > 0) {
            result.set(Result.FAIL, "参数key不能重复！", null);
            return 0;
        }
        //end by bobby
        int ret = systemParamsDao.save(systemParams);
        if (ret == 0) {
            result.set(Result.FAIL, "保存失败", null);
        }
        SystemUtil.setSystemParams(systemParams());
        return ret;
    }

    @Override
    public int update(Result result, SystemParamsDO systemParams) {
        //add by bobby
        if (StringUtils.isBlank(systemParams.getParamKey())) {
            result.set(Result.FAIL, "参数key不能为空！", null);
            return 0;
        }
        Map map = Maps.newHashMap();
        map.put("paramKey", systemParams.getParamKey());
        List<SystemParamsDO> systemParamsDOlist = systemParamsDao.list(map);
        List<SystemParamsDO> repeatList = systemParamsDOlist.stream().filter(dbSystemParamDo -> !systemParams.getId().equals(dbSystemParamDo.getId())).collect(Collectors.toList());
        if (repeatList.size() > 0) {
            result.set(Result.FAIL, "参数key不能重复！", null);
            return 0;
        }
        //end by bobby
        int ret = systemParamsDao.update(systemParams);
        if (ret == 0) {
            result.set(Result.FAIL, "更新失败", null);
        }
        SystemUtil.setSystemParams(systemParams());
        return ret;
    }

    @Override
    public int remove(Result result, Integer id) {
        int ret = systemParamsDao.remove(id);
        if (ret == 0) {
            result.set(Result.FAIL, "删除失败", null);
        }
        SystemUtil.setSystemParams(systemParams());
        return ret;
    }

    @Override
    public int batchRemove(Result result, Integer[] ids) {
        int ret = systemParamsDao.batchRemove(ids);
        if (ret != ids.length) {
            result.set(Result.FAIL, "部分删除失败", null);
        }
        SystemUtil.setSystemParams(systemParams());
        return ret;
    }

    @Override
    public Map<String, SystemParamsDO> systemParams() {
        List<SystemParamsDO> list = systemParamsDao.list(Maps.newHashMap());
        Map<String, SystemParamsDO> map = list.stream().collect(Collectors.toMap(SystemParamsDO::getParamKey, a -> a, (k1, k2) -> k1));
        return map;
    }

}
